#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2018/10/12 11:18
@desc
"""

import re, urllib2

headers = ("User-Agent",
           "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.22 Safari/537.36 SE 2.X MetaSr 1.0")
opener = urllib2.build_opener()
opener.addheaders = [headers]
# 安装为全局
urllib2.install_opener(opener)
data = urllib2.urlopen(
    url="http://www.thebigproxylist.com/members/proxy-api.php?output=all&user=list&pass=8a544b2637e7a45d1536e34680e11adf"
).read().decode('utf8')
ippool = data.split('\n')
print(len(ippool))

with open('normal-ips.txt', 'w') as f:
    for ip in ippool:
        ip = ip.decode('utf-8').encode('ascii', 'ignore')
        ip = ip.split(',')[0]
        try:
            print("当前代理IP:" + ip)
            proxy = urllib2.ProxyHandler({"http": ip})
            opener = urllib2.build_opener(proxy, urllib2.HTTPHandler)
            urllib2.install_opener(opener)
            url = "http://www.baidu.com"
            request = urllib2.Request(url)
            request.add_header('User-Agent', 'fake-client')
            response = urllib2.urlopen(request, timeout=5).read().decode('utf-8', 'ignore')

            # print(response)
            print("通过")
            f.write(ip + '\n')
            f.flush()
            print("-----------------------------")
        except Exception as err:
            print(err)
            print("-----------------------------")

